<?php

namespace App\Service\Kms\Album;

use App\Events\Kms\GroupShareEvent;
use App\Library\FeiShu\SendFs;
use App\Models\Kms\Album\AlbumMainModel;
use App\Models\Kms\Album\AlbumResourceModel;
use App\Remote\Feishu\FeishuImageRemote;
use App\Service\Kms\Common\DateService;
use App\Service\Kms\KmsBaseService;
use App\Service\OpenShop\Feishu\ConfigService;
use Illuminate\Support\Collection;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class AlbumMainService extends KmsBaseService
{
    /**
     * @return array|void
     * @throws ValidationException
     */
    public static function main()
    {
        try {
            return [
                "albumMain" => AlbumMainModel::detail(request()->user()->rmsUserId)
            ];
        } catch (\Exception $exception) {
            handleException($exception, null, null, "获取相册主信息失败");
        }
    }

    /**
     * @param bool $withConfig
     * @return array|void
     * @throws ValidationException
     */
    public static function groupList(bool $withConfig = false)
    {
        try {
            return [
                "groupList" => collect(ConfigService::getConfigDetail("kms.shareGroup", "groupList",
                        30 * 60)["value"] ?? [])
                    ->map(function ($v, $key) use ($withConfig) {
                        return collect($v)->when(!$withConfig, function ($query) {
                            return $query->only(["groupName", "icon", "departmentId"]);
                        })->merge(["id" => $key])->toArray();
                    })->values()->toArray()
            ];
        } catch (\Exception $exception) {
            handleException($exception, null, null, "获取分享群列表失败");
        }
    }


    /**
     * @param object $params
     */
    public static function groupShare(object $params)
    {
        event(
            new GroupShareEvent(
                $params->groupIds,
                $params->resourceIds,
                $params->mid,
                attributeGet($params, "comment", ""),
                auth()->user()->toArray()
            )
        );
    }
}
